#include "pagepisstationselect.h"
#include "ui_pagepisstationselect.h"

PagePisStationSelect::PagePisStationSelect(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PagePisStationSelect)
{
    ui->setupUi(this);
    setAutoFillBackground(true);  //设置自动填充
    QPalette palette = BKGCLR;
    setPalette(palette);

    m_nCurLineID = 0;
    m_nSelecMode = Select_StartStation;
    ui->RouteName->setText(QString("%1     开往     %2").arg(GlobalConfig::stationList[m_nCurLineID].startName).arg(GlobalConfig::stationList[m_nCurLineID].destName));
    InitStationBtns();
    setPageActive(false);
}

PagePisStationSelect::~PagePisStationSelect()
{
    delete ui;
}

void PagePisStationSelect::setPageActive(bool xActive)
{
    ui->Btn_PisDestStation->setEnabled(xActive);
    ui->Btn_PisNextStation->setEnabled(xActive);
    ui->Btn_PisStartStation->setEnabled(xActive);
    ui->Btn_PisStationConfirm->setEnabled(xActive);
    ui->Btn_PisDepartConfirm->setEnabled(xActive);

    for(int i=0;i<m_lStationBtnList.length();i++)
        m_lStationBtnList[i]->setEnabled(xActive);
}

void PagePisStationSelect::InitStationBtns()
{
    m_nColsInRow = 5;
    uint stationCnt = GlobalConfig::stationList[m_nCurLineID].stationCount;
    m_nStartx = ui->label->pos().x();
    m_nStarty = ui->label->pos().y() + 70;
    uint nAreaWidth = ui->frame->width()-2*ui->label->pos().x();
    m_nStationLabelLen  = 160;
    m_nStationLabelHeight = 50;
    m_nHSpace = (nAreaWidth - m_nColsInRow*m_nStationLabelLen)/(m_nColsInRow-1);
    m_nVSpace = 10;

    m_pStationBtnMapper = new QSignalMapper(this);
    for(uint i=0;i<stationCnt;i++)
    {
        QPushButton *stationBtn = new QPushButton(ui->frame);
        stationBtn->setStyleSheet(StationBtnStyle);
        stationBtn->setGeometry(m_nStartx+(i%m_nColsInRow)*(m_nStationLabelLen+m_nHSpace),
                                m_nStarty+(i/m_nColsInRow)*(m_nStationLabelHeight+m_nVSpace),
                                m_nStationLabelLen,m_nStationLabelHeight);
        stationBtn->setText(GlobalConfig::stationList[m_nCurLineID].station.at(i).name);
        m_lStationBtnList.append(stationBtn);
        connect(stationBtn, SIGNAL(clicked()), m_pStationBtnMapper, SLOT(map()));
        m_pStationBtnMapper->setMapping(stationBtn, i);
    }
    connect(m_pStationBtnMapper, SIGNAL(mapped(int)),this, SLOT(slotStationBtnClick(int)));
}

void PagePisStationSelect::slotStationBtnClick(int index)
{
    if(m_nSelecMode == Select_Nothing)
        return;
    else if(m_nSelecMode == Select_StartStation)
    {
        start_station_id_ = GlobalConfig::stationList[m_nCurLineID].station.at(index).stationId;
        ui->Btn_PisStartStation->setText(GlobalConfig::stationList[m_nCurLineID].station.at(index).name);
    }
    else if(m_nSelecMode == Select_CurrentStation)
    {
        cur_station_btn_index_ = index;
        cur_station_id_ = GlobalConfig::stationList[m_nCurLineID].station.at(index).stationId;
        ui->Btn_PisNextStation->setText(GlobalConfig::stationList[m_nCurLineID].station.at(index).name);
    }
    else if(m_nSelecMode == Select_DestStation)
    {
        end_station_id_ = GlobalConfig::stationList[m_nCurLineID].station.at(index).stationId;
        ui->Btn_PisDestStation->setText(GlobalConfig::stationList[m_nCurLineID].station.at(index).name);
    }
}

void PagePisStationSelect::on_Btn_PisStartStation_pressed()
{
    m_nSelecMode = Select_StartStation;
}

void PagePisStationSelect::on_Btn_PisNextStation_pressed()
{
    m_nSelecMode = Select_CurrentStation;
}

void PagePisStationSelect::on_Btn_PisDestStation_pressed()
{
    m_nSelecMode = Select_DestStation;
}

void PagePisStationSelect::ChangeLineInfo(uint8_t id)
{
    m_nCurLineID = id-1;
    if(int(m_nCurLineID) >= GlobalConfig::stationList.length() || int(m_nCurLineID) < 0)
        return;
    ui->RouteName->setText(QString("%1     开往     %2").arg(GlobalConfig::stationList[m_nCurLineID].startName).arg(GlobalConfig::stationList[m_nCurLineID].destName));
    for(int i=0;i<m_lStationBtnList.length();i++)
    {
        m_lStationBtnList[i]->setText(GlobalConfig::stationList[m_nCurLineID].station.at(i).name);
    }
    ui->Btn_PisStartStation->setText("");
    ui->Btn_PisNextStation->setText("");
    ui->Btn_PisDestStation->setText("");
}

void PagePisStationSelect::sendStationSetToVCU()
{
    emit signal_PisStationSet(true);
    ui->Btn_PisStationConfirm->setEnabled(false);
    ui->Btn_PisDepartConfirm->setEnabled(false);
    QTimer::singleShot(3000,this,[=](){
        emit signal_PisStationSet(false);
        ui->Btn_PisStationConfirm->setEnabled(true);
        ui->Btn_PisDepartConfirm->setEnabled(true);
    });
}

void PagePisStationSelect::sendDepartToVCU()
{
    emit signal_DepartComfirm(true);
    ui->Btn_PisStationConfirm->setEnabled(false);
    ui->Btn_PisDepartConfirm->setEnabled(false);
    QTimer::singleShot(3000,this,[=](){
        emit signal_DepartComfirm(false);
        ui->Btn_PisStationConfirm->setEnabled(true);
        ui->Btn_PisDepartConfirm->setEnabled(true);
    });
}

void PagePisStationSelect::on_Btn_PisStationConfirm_clicked()
{
    if(StationSelectIsEmpty())
        return;
    sendStationSetToVCU();
}

void PagePisStationSelect::slotNextStationPA()
{
    if(StationSelectIsEmpty())
        return;
    if(cur_station_btn_index_ < m_lStationBtnList.length()-1)
    {
        cur_station_btn_index_++;
        ui->Btn_PisNextStation->setText(m_lStationBtnList.at(cur_station_btn_index_)->text());
        cur_station_id_ = GlobalConfig::stationList[m_nCurLineID].station.at(cur_station_btn_index_).stationId;
    }
    sendStationSetToVCU();
}

void PagePisStationSelect::slotPreStationPA()
{
    if(StationSelectIsEmpty())
        return;
    if(cur_station_btn_index_ > 0)
    {
        cur_station_btn_index_--;
        ui->Btn_PisNextStation->setText(m_lStationBtnList.at(cur_station_btn_index_)->text());
        cur_station_id_ = GlobalConfig::stationList[m_nCurLineID].station.at(cur_station_btn_index_).stationId;
    }
    sendStationSetToVCU();
}

void PagePisStationSelect::on_Btn_PisDepartConfirm_clicked()
{
    if(StationSelectIsEmpty())
        return;
    sendDepartToVCU();
}

int PagePisStationSelect::StationSelectIsEmpty()
{
    if(ui->Btn_PisStartStation->text().isEmpty())
    {
        PopupDialog *dialog = new PopupDialog();
        dialog->move(this->x()+(this->width()-dialog->width())/2,
                     this->y()+(this->height()-dialog->height())/2);
        dialog->setPopupText("请输入起点站！");
        dialog->exec();
        return 1;
    }
    if(ui->Btn_PisNextStation->text().isEmpty())
    {
        PopupDialog *dialog = new PopupDialog();
        dialog->move(this->x()+(this->width()-dialog->width())/2,
                     this->y()+(this->height()-dialog->height())/2);
        dialog->setPopupText("请输入当前站！");
        dialog->exec();
        return 2;
    }
    if(ui->Btn_PisDestStation->text().isEmpty())
    {
        PopupDialog *dialog = new PopupDialog();
        dialog->move(this->x()+(this->width()-dialog->width())/2,
                     this->y()+(this->height()-dialog->height())/2);
        dialog->setPopupText("请输入终点站！");
        dialog->exec();
        return 3;
    }
    return 0;
}
